package yunhe.biz.impl;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import yunhe.biz.StockMgrBiz;
import yunhe.dao.IStockMgrDao;
import yunhe.entity.*;
import yunhe.util.SqlSessionFactoryUtil;

import java.util.ArrayList;
import java.util.List;

public class StockMgrBizImpl implements StockMgrBiz {
    private static SqlSession se=SqlSessionFactoryUtil.getSqlSession();
    private static IStockMgrDao dao=se.getMapper(IStockMgrDao.class);
    /**
     *
     * 查询所有仓库
     */
    public List<Storage> querySto()
    {

        return dao.querySto();
    }

    /**
     * 根据仓库id查询商品信息及其对应库存
     */
    public List<Inventory> searchInvBySto(String stoId)
    {

        return dao.searchInvBySto(stoId);

    }
    /**
     * 在盘点表中根据仓库id查询所有损益商品
     */
    public  List<Taking> queryProLossPro(String stoId)
    {
        return dao.queryProLossPro(stoId);
    }
    /**
     * 根据仓库名获取仓库信息
     */
    public  List<Storage> queryStoByName(String stoName)
    {
        return dao.queryStoByName(stoName);
    }
    /**
     * 根据仓库id和商品条形码查询商品盘点信息
     */
    public  List<Taking> queryProLossByStoAndPro(String stoId,String takId,String proBarCode)

    {
        return dao.queryProLossByStoAndPro(stoId,takId,proBarCode);
    }
    /**
     * 报损报溢，插入损益表
     */
    public int  insertToProLoss(Object[] params)
    {
        int i=dao.insertToProLoss(params);
        se.commit();
        return i;

    }
    /**
     * 查询所有的采购信息
     */
    public List<Purchase> quryAllPurchaseMsg()
    {
        return dao.quryAllPurchaseMsg();
    }
    /**
     * 插入盘点详情表
     */
    public int insertIntoTaking(@Param("param") Object[] params)
    {
        int i=dao.insertIntoTaking(params);
        se.commit();
        return i;
    }

    /**
     * 根据仓库id和盘点单号查询盘点信息
     */
    public List<Taking> queryProByStoTakId( String takId,String stoid)
    {
    	return dao.queryProByStoTakId(takId, stoid);
    }

    @Override
    public List<Inventory> searchInvByPro(String proBarCode) {
        return dao.searchInvByPro(proBarCode);
    }

    @Override
    public List<Inventory> searchInv() {
        return dao.searchInv();
    }

    @Override
    public int setInvLimit(String sto, String proBarCode, String topNum, String buttNum) {
        int re1=dao.setInvTopLimit(Integer.parseInt(topNum),sto,proBarCode);
        int re2=dao.setInvButtLimit(Integer.parseInt(buttNum),sto,proBarCode);
        if(re1>0&&re2>0){
            se.commit();
        }
        else{
            se.rollback();
        }
        return re1*re2;
    }

    @Override
    public int transInv(StockTransfer it) {

        int re=dao.transInv(it);
        if(re>0){
            se.commit();
        }
        else{
            se.rollback();
        }
        return re;
    }
    /**
     * 写入入库单
     */
	public int stockIn(StockIn si,List<StockInAppend> list)
	{
		int r1=dao.writeStockIn(si);
		int r2=0;
		int r3=0;
		for(StockInAppend s:list)
		{
			r2=dao.writeStockInApp(s);
			System.out.println(s.getStockInQuantity());
			if (r2<=0||r3<=0){
                break;
            }
        }
        if(r1>0&&r2>0&&r3>=0){
            //更新销单执行状态为1
            se.commit();
        }
        else{
            se.rollback();
        }
        return r1*r2;
	}
    @Override
    public int stockOut(StockOut sk, List<StockOutAppend> lsoa) {



        int re1=dao.writeStockOut(sk);
        int re2=0;
        int re3=0;
        for(StockOutAppend soa:lsoa){
            re2=dao.writeStockOutApp(soa);
            re3=dao.updateInv(soa.getProBarCode(),soa.getStoId(),soa.getStockOutQuantity());
            if (re2<=0||re3<=0){
                break;
            }
        }
        if(re1>0&&re2>0&&re3>=0){
            //更新销单执行状态为1
            se.commit();
        }
        else{
            se.rollback();
        }
        return re1*re2*re3;
    }

    public List<Taking> queryLatestTimeTak()
    {
    	return dao.queryLatestTimeTak();
    }

	   /**
  * 查询所有的入库信息
  *
  */

 public Object[] queryAllStockInAppend()
 {
	 List<Object> listdata=new ArrayList<Object>();

	List<StockInAppend> list=dao.queryAllStockInAppend();
	for(StockInAppend l:list)
	{
		if(l.getProBarCode()!=null)
		{
		String proBarCode=l.getProBarCode();
		Product product=dao.searchProBy(proBarCode);
		listdata.add(product);
		}
	}
	Object[] o={list,listdata};
	return o;
 }
 /**
  * 查询所有的出库单信息
  *
  */
 public Object[] queryAllStockOutAppend()
 {
 List<Object> listdata=new ArrayList<Object>();
 List<StockOutAppend> list=dao.queryAllStockOutAppend();
 for(StockOutAppend l:list)
	{
		if(l.getProBarCode()!=null)
		{
		String proBarCode=l.getProBarCode();
		Product product=dao.searchProBy(proBarCode);
		listdata.add(product);
		}
	}
	Object[] o={list,listdata};
	return o;

 }

 /**
  * 根据时间查询入库信息
  *
  */
 public Object[] queryStockInBydate(String date1,String date2,String xuanze)
 {
	 List<Object> listdata=new ArrayList<Object>();
	 List<StockInAppend> list=dao.queryStockInBydate(date1, date2);
	 for(StockInAppend l:list)
		{
			if(l.getProBarCode()!=null)
			{
			String proBarCode=l.getProBarCode();
			Product product=dao.searchProBy(proBarCode);
			listdata.add(product);
			}
		}
		Object[] o={list,listdata};
		return o;
 }

 /**
  * 根据时间查询出库信息
  */
 public Object[] queryStockOutBydate(String date1,String date2,String xuanze)
 {	 List<Object> listdata=new ArrayList<Object>();

	 if(date1!=null&&date2!=null&&xuanze.equals("1"))
	 {
		 List<StockInAppend> list=dao.queryStockInBydate(date1, date2);
		 for(StockInAppend l:list)
			{
				if(l.getProBarCode()!=null)
				{
				String proBarCode=l.getProBarCode();
				Product product=dao.searchProBy(proBarCode);
				listdata.add(product);
				}
			}
			Object[] o={list,listdata};
			return o;
	 }
	 else if(date1!=null&&date2!=null&&xuanze.equals("2"))
	 {
		 List<StockOutAppend> list=dao.queryStockOutBydate(date1, date2);
		 for(StockOutAppend l:list)
			{
				if(l.getProBarCode()!=null)
				{
				String proBarCode=l.getProBarCode();
				Product product=dao.searchProBy(proBarCode);
				listdata.add(product);
				}
			}
			Object[] o={list,listdata};
			return o;
	 }
	 else if(xuanze.equals("1")&&(date1==null||date2==null))
	 {
		 List<StockInAppend> list=dao.queryAllStockInAppend();
		 for(StockInAppend l:list)
			{
				if(l.getProBarCode()!=null)
				{
				String proBarCode=l.getProBarCode();
				Product product=dao.searchProBy(proBarCode);
				listdata.add(product);
				}
			}
			Object[] o={list,listdata};

			return o;
	 }
	 else if(xuanze.equals("2")&&(date1==null||date2==null))
	 {
		 List<StockOutAppend> list=dao.queryAllStockOutAppend();

		 for(StockOutAppend l:list)
			{
				if(l.getProBarCode()!=null)
				{
				String proBarCode=l.getProBarCode();
				Product product=dao.searchProBy(proBarCode);
				listdata.add(product);
				}
			}
			Object[] o={list,listdata};
			return o;
	 }
	return null;



 }

}
